import {PageContainer} from "@ant-design/pro-components";
import DictTypeTable from "@/pages/DictType/components/DictTypeTable";
import {DictTypeVO, saveDictType, updateDictType} from "@/services/DictTypeService";
import DictTypeUpdateModal from "@/pages/DictType/components/DictTypeUpdateModal";
import {useState} from "react";
import {message} from "antd";

const DictType = () => {
    // 编辑操作
    const [updateModalState, setUpdateModalState] = useState(false)
    const [currentDictType, setCurrentDictType] = useState<DictTypeVO | undefined>()
    const updateEvent = async (record: DictTypeVO) => {
        // 打开模态框
        setCurrentDictType(record)
        setUpdateModalState(true)
    }

    const saveEvent = async () => {
        // 打开模态框
        setUpdateModalState(true)
    }

    const onUpdateModalChange = async (state: boolean) => {
        if (state) {
            setUpdateModalState(state);
        } else {
            await setCurrentDictType(undefined)
            setUpdateModalState(state)
        }
    }
    // 提交修改
    const onUpdateFinish = async (formData: any) => {
        let response
        if (currentDictType) {
            response = await updateDictType(formData);
        } else {
            response = await saveDictType(formData)
        }
        if (response) {
            message.success("操作成功")
            return true
        }
        return false;
    }

    return <PageContainer title="字典类型">
        {/*字典类型表格 开始*/}
        <DictTypeTable onSave={saveEvent} onUpdate={updateEvent}/>
        {/*字典类型表格 结束*/}

        {/*修改模态框 开始*/}
        <DictTypeUpdateModal
                dictType={currentDictType}
                open={updateModalState}
                onOpenChange={onUpdateModalChange}
                onFinish={onUpdateFinish}/>
        {/*修改模态框 结束*/}
    </PageContainer>
}

export default DictType
